//
//  TZArenaViewController.m
//  wangYiCaiPiao
//
//  Created by mac on 16/1/4.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZArenaViewController.h"

@interface TZArenaViewController ()
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) UIWebView *oneWebView;
@property (nonatomic, strong) UIWebView *twoWebView;
@end

@implementation TZArenaViewController

//懒加载 - 避免重复调用
- (UIWebView *)webView {
    if (_webView == nil) {
        _webView = [[UIWebView alloc] init];
    }
    return _webView;
}

- (NSArray *)array {
    if (_array == nil) {
        _array = [NSArray arrayWithObjects:@"篮球",@"足球", nil];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
}

//更新UI
- (void)setupUI {
    [self addSegmentedControlInController];
}

//添加选择条
- (void)addSegmentedControlInController {
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, 100, 25);
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:self.array];
    [self.view addSubview:segmentedControl];
    self.navigationItem.titleView = view;
    [view addSubview:segmentedControl];
    [segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view);
    }];
    //设置导航条背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NLArenaNavBar64"] forBarMetrics:UIBarMetricsDefault];
    //设置控制器的背景图片
    //    self.view.layer.contents = (__bridge id)([UIImage imageNamed:@"NLArenaBackground"].CGImage);
    //设置默认的背景图片
    [segmentedControl setBackgroundImage:[UIImage imageNamed:@"CPArenaSegmentBG"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    //设置选中的背景图片
    [segmentedControl setBackgroundImage:[UIImage imageNamed:@"CPArenaSegmentSelectedBG"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    //设置文字默认的颜色
    [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    //设置选中文字的颜色
    [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
    [segmentedControl setTintColor:[UIColor clearColor]];
    segmentedControl.selectedSegmentIndex = 0;
    [self addHtml:@"http://caipiao.163.com/m/search.jsp"]; //默认加载界面
    [segmentedControl addTarget:self action:@selector(segmentedControlDidClick:) forControlEvents:UIControlEventValueChanged];
}

//选择条的监听事件
- (void)segmentedControlDidClick:(UISegmentedControl *)Seg {
    NSInteger Index = Seg.selectedSegmentIndex;
    switch (Index) {
        case 0:
            [self addHtml:@"http://caipiao.163.com/m/search.jsp"];
            break;
        case 1:
            [self addHtml:@"http://caipiao.163.com/nfop/nteshlg/index.htm"];
            break;
        default:
            break;
    };
}

//添加选择界面
- (void)addHtml:(NSString *)htmlString {
    [self.view addSubview:self.webView];
    self.webView.layer.contents = (__bridge id)([UIImage imageNamed:@"NLArenaBackground"].CGImage);
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.webView.opaque = NO;
    self.webView.backgroundColor = [UIColor whiteColor];
    NSURL *urlString = [NSURL URLWithString:htmlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:urlString];
    [self.webView loadRequest:request];
}

@end


